define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
	$scope.form.fk_unit = 'self';

	if(params.father_id!='undefined'){
		httpService.post(app.urls.menu_select,{"id":params.father_id},function(data){
			if("0000" === data.code){
				$scope.menu_name = data.data[0].name;
				$scope.findButton(data.data[0].id);
			}else{
				alert(data.msg);
			}
		});
	}else {
		$scope.form.sheet=params.sheet;
		$scope.form.field=params.field;
		$scope.form.type=params.type;
	}
	$scope.findButton=function(menu_id){
		httpService.post(app.urls.button_select,{"menu_id":menu_id},function(data){
			if("0000" === data.code){
				$scope.buttons = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	//学生状态
	httpService.post(app.urls.dictionary_selectByRule,{fk_rule:"SJAAAAA10061"},function(data){
		if("0000" === data.code){
			$scope.stateList = data.data;
		}else{
			alert(data.msg);
		}
	});
	httpService.post(app.urls.unit_selectSchoolAndAttach,{"fk_unit":$scope.form.fk_unit},function(data){
		if("0000" === data.code){
			$scope.oldList = data.data;
			if($scope.oldList.length>1){$scope.form.attach=true;}else{$scope.form.attach=false;}
		}else{
			alert(data.msg);
		}
	});
	//查询年级
	$scope.findGrade = function(){
		if($scope.form.old_pk == null) {
			$scope.form.fk_unit = "self";
		}else{
			$scope.form.fk_unit = $scope.form.old_pk;
		}
		httpService.post(app.urls.grade_selectCenterGrade,{"fk_unit":$scope.form.fk_unit},function(data){
			if("0000" === data.code){
				$scope.gradeList = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	$scope.findGrade();
	//获取班级
	$scope.findClass=function(){
		httpService.post(app.urls.class_selectClassName,{"fk_unit":$scope.form.fk_unit,"fk_grade":$scope.form.fk_grade,"status":'0'},function(data){
			if("0000" === data.code){
				$scope.classList = data.data;
				$scope.findClassTeacher();
			}else{
				alert(data.msg);
			}
		});
	}
	$scope.findClass();

	/**
	 * 查询班主任班级
	 * 对班级进行权限控制。
	 * 班主任只能查看本班级的学生。
	 */
	$scope.findClassTeacher = function(){
		httpService.post(app.urls.findClassTeacherURL, {}, function(data){
			if("0000" === data.code){
				$scope.classTeacherList = data.data;
				// 判断是否查找到班主任的班级（没找到班级，说明是管理员。找到班级，说明是班主任。）
				if ($scope.classTeacherList.length == 0){
					$scope.classList.splice(0,0,{id:"",name:"==请选择=="});
					$scope.form.fk_class = "";
				}else {
					// 班主任使用，需要将其他班级过滤掉。
					for (let i = $scope.classList.length-1; i >= 0; i--) {
						let classObj = $scope.classList[i];
						let existFlag = false;
						for (let j = 0; j < $scope.classTeacherList.length; j++) {
							let classTeacher = $scope.classTeacherList[j];
							if (classObj.id == classTeacher.fk_class){
								existFlag = true;
								break;
							}
						}
						if (!existFlag){
							$scope.classList.splice(i,1);
						}
					}
					$scope.classList.splice(0,0,{id:"emptyId",name:"==请选择=="});
					$scope.form.fk_class = "emptyId";
				}
				$scope.findRoleType();
			}else{
				alert(data.msg);
			}
		});
	}

	//查询学生列表
	$scope.find = function(){
		$scope.form.list='list';
		httpService.postpage(app.urls.student_select,$scope.form,function(data){
			if("0000" === data.code){
    			$scope.rows = data.data;
    		}else{
    			alert(data.msg);
    		}
		});
	}

	//按钮点击事件-条件查询
	$scope.select = function(){
    	$scope.page.current = 1;
    	$scope.find();
    }

	$scope.findRoleType = function() {
		httpService.post(app.urls.role_selectType, {}, function (data) {
			if ("0000" === data.code) {
				$scope.form.userrole = data.data[0].type;
				$scope.find();
			} else {
				alert(data.msg);
			}
		});
	}


	$scope.clickBtn=function(event){
		var menu = {};
		if(event=='add'){
			menu.url = "modules/content/usermanagement/student/add?fk_menuid="+params.fk_menuid+ "&father_id=" + params.father_id;
			if(params.father_id!='undefined'){
				eventBusService.publish("modules.content.load",menu);
			}else {
				eventBusService.publish("modules.content.load",menu);
			}
		}
		if(event=='info'){
			var values = [];
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				values.push($(this).val());
			});
			if(values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择查看数据。"});
			}else if(values.length > 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
			}else {
				menu.url = "modules/content/usermanagement/student/info?id=" + values[0] + "&father_id=" + params.father_id;
				if(params.father_id!='undefined'){
					eventBusService.publish("modules.content.load",menu);
				}else {
					eventBusService.publish("modules.content.load",menu);
				}
			}
		}
		if(event=='update'){
			var values = [];
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				values.push($(this).val());
			});
			if(values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
			}else if(values.length > 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
			}else {
				menu.url = "modules/content/usermanagement/student/update?id=" + values[0] + "&father_id=" + params.father_id+ "&sheet=" + params.sheet+ "&field=" + params.field+ "&type=" + params.type;
				if(params.father_id!='undefined'){
					eventBusService.publish("modules.content.load",menu);
				}else {
					eventBusService.publish("modules.content.load",menu);
				}
			}
		}
		if(event=='delete'){
			var values = [];
			var ids="";
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				var arr=$(this).val().split(',');
				values.push(arr[0]);
				ids = ids+",'"+arr[0]+"'";
			});
			if(values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
			}else{
				eventBusService.publish("modal.alert",{"content":"你是否确认删除这"+values.length+"数据？","isAutoClose":false,"confirm":function(){
						httpService.post(app.urls.user_delete,{"id":ids.replace(',','')},function(data){
							if("0000" === data.code){
								httpService.post(app.urls.student_delete,{"id":ids.replace(',','')}, function(data) {
									if ("0000" === data.code) {
										httpService.post(app.urls.error_delete,{"record":ids.replace(',','')}, function(data) {
											if ("0000" === data.code) {
												$scope.select();
											} else {
												alert(data.msg);
											}
										});
									} else {
										alert(data.msg);
									}
								});
							}else{
								alert(data.msg);
							}
						});
					},"cancel":function(){
						//用户取消，不做操作
					}});
			}
		}
		if(event=='export'){

			httpService.post(app.urls.student_exportXj,$scope.form, function(data) {
				if ("0000" === data.code) {
					window.location.href = data.data;
				} else {
					alert(data.msg);
				}
			});
		}
		if(event=='template'){
			httpService.post(app.urls.student_template,{}, function(data) {
				if ("0000" === data.code) {
					window.location.href = data.data;
				} else {
					alert(data.msg);
				}
			});
		}
		if(event=='import'){
			eventBusService.publish("modal.page","modules/content/usermanagement/student/import");
		}
		
		if(event=='resetPwd'){
			var values = [];
			var ids="";
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				var arr=$(this).val().split(',');
				values.push(arr[0]);
				ids = ids+","+arr[0]+"";
			});
			if(values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
			}else{
				eventBusService.publish("modal.alert",{"content":"你是否重置这"+values.length+"名用户密码？","isAutoClose":false,"confirm":function(){
					
					httpService.post(app.urls.user_resetPwd,{"ids":ids.replace(',',''),"type":'2'}, function(data) {
						if ("0000" === data.code) {
							console.info(data.data=="");
							if(data.data==""){
								alert(data.msg);
							}else{
								alert(data.msg+"但其中["+data.data+"]密码重置失败!")
							}
						} else {
							alert(data.msg);
						}
					});
					
					},"cancel":function(){
						//用户取消，不做操作
					}});
			}
		}
	}
	//返回
	$scope.goback = function(){
		var menu = {"url": "modules/content/welcome/school"};
		eventBusService.publish("modules.content.load", menu);
	}
	//接收数据刷新
	eventBusService.subscribe('content.page.reload', function(event,data) {
		$scope.select();
	});
		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAll = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
}}});